<?php

namespace lib;

trait Singleton
{

    private static $instance = [];

    /**
     * 加载单例模式
     * @return mixed|static
     */
    public static function getInstance()
    {
        $class = get_called_class();// soon will be replaced by static::class
        if (!isset(self::$instance[$class])) {
            $args = func_get_args();
            $argc = func_num_args();
            switch ($argc) {
                case 0:
                    self::$instance[$class] = new static();
                    break;
                case 1:
                    self::$instance[$class] = new static($args[0]);
                    break;
                case 2:
                    self::$instance[$class] = new static($args[0], $args[1]);
                    break;
                case 3:
                    self::$instance[$class] = new static($args[0], $args[1], $args[2]);
                    break;
                default:
                    trigger_error(__METHOD__ . " {$class} please keep less 4 params!", E_USER_WARNING);
                    break;
            }
        }
        return self::$instance[$class];
    }

}
